<template>
  <div class="page-wrap">
    <el-card shadow="never">
      <div class="toolbar">
        <el-input v-model="filters.search" placeholder="搜索名称/联系人/电话" clearable class="w-300" />
        <el-button type="primary" @click="openEdit()">新增供应商</el-button>
        <el-button @click="exportCsv">导出</el-button>
      </div>
      <el-table :data="dataList" border stripe>
        <el-table-column prop="name" label="名称" min-width="180" />
        <el-table-column prop="contact" label="联系人" width="120" />
        <el-table-column prop="phone" label="电话" width="140" />
        <el-table-column prop="address" label="地址" min-width="200" />
        <el-table-column prop="settle" label="结算方式" width="120" />
        <el-table-column label="操作" width="160" fixed="right">
          <template #default="{ row }">
            <el-button size="small" @click="openEdit(row)">编辑</el-button>
            <el-button size="small" type="danger" @click="onDelete(row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <el-dialog v-model="visible" :title="form.id?'编辑供应商':'新增供应商'" width="600px">
      <el-form :model="form" label-width="100px">
        <el-form-item label="名称"><el-input v-model="form.name" /></el-form-item>
        <el-form-item label="联系人"><el-input v-model="form.contact" /></el-form-item>
        <el-form-item label="电话"><el-input v-model="form.phone" /></el-form-item>
        <el-form-item label="地址"><el-input v-model="form.address" /></el-form-item>
        <el-form-item label="结算方式"><el-select v-model="form.settle"><el-option label="现结" value="现结"/><el-option label="月结" value="月结"/></el-select></el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="visible=false">取消</el-button>
        <el-button type="primary" @click="onSave">保存</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { computed, onMounted, reactive, ref } from 'vue'
import { useSupplierStore } from '@/stores/supplier.js'
const store = useSupplierStore()
const filters = reactive({ search: '' })
const visible = ref(false)
const form = reactive({ id: '', name: '', contact: '', phone: '', address: '', settle: '现结' })
onMounted(()=>{ store.initialize() })

const dataList = computed(()=>{
  const s = (filters.search||'').toLowerCase()
  return store.suppliers.filter(x=>{
    if(!s) return true
    return x.name.toLowerCase().includes(s) || (x.contact||'').toLowerCase().includes(s) || (x.phone||'').includes(filters.search)
  })
})

function openEdit(row){
  Object.assign(form, row || { id:'', name:'', contact:'', phone:'', address:'', settle:'现结' })
  visible.value = true
}
function onSave(){
  if(form.id) store.update(form.id, form)
  else store.add(form)
  visible.value = false
}
function onDelete(row){ store.remove(row.id) }
function exportCsv(){
  const headers = ['名称','联系人','电话','地址','结算方式']
  const rows = store.suppliers.map(s=>[s.name,s.contact,s.phone,s.address,s.settle])
  const csv = [headers.join(','), ...rows.map(r=>r.join(','))].join('\n')
  const blob = new Blob(['\ufeff' + csv], { type: 'text/csv;charset=utf-8;' })
  const link = document.createElement('a')
  link.href = URL.createObjectURL(blob)
  link.download = `供应商_${new Date().toISOString().slice(0,10)}.csv`
  link.click()
}
</script>

<style scoped>
.page-wrap { padding: 16px; }
.toolbar { display: flex; gap: 12px; margin-bottom: 12px; }
.w-300 { width: 300px; }
</style>


